/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.support;

import io.gitee.jaemon.spring.adorner.core.AdornerProperty;
import io.gitee.jaemon.spring.adorner.core.ApiParamShow;
import io.gitee.jaemon.spring.adorner.exception.AdornerException;
import io.gitee.jaemon.spring.adorner.type.ParamPair;
import io.gitee.jaemon.spring.adorner.util.MarkdownUtils;
import io.gitee.jaemon.spring.adorner.util.StringUtils;

import static io.gitee.jaemon.spring.adorner.AdornerConstants.*;
import static io.gitee.jaemon.spring.adorner.support.MarkdownHelper.EMPTY_TABLE;
import static io.gitee.jaemon.spring.adorner.util.MarkdownUtils.LINE_BREAK;

/**
 * API接口参数展示-markdown形式
 *
 * @author Jaemon
 * @since 1.0
 */
public enum MarkdownApiShow implements ApiParamShow<ParamPair> {
    /**
     * JSON报文形式
     */
    JSON {
        @Override
        public String request(ParamPair paramPair) throws AdornerException {
            StringBuilder request = new StringBuilder();

            request.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_REQUEST_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            MarkdownUtils.jsonCode(paramPair.json())
                    ).append(NEWLINE);

            return request.toString();
        }

        @Override
        public String response(ParamPair paramPair) throws AdornerException {

            StringBuilder response = new StringBuilder();
            response.append(MARKDOWN_ITEM)
                    .append(
                        AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_RESPONSE_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            MarkdownUtils.jsonCode(paramPair.json())
                    ).append(LINE_BREAK);

            return response.toString();
        }
    },
    /**
     * 表格形式
     */
    TABLE {
        @Override
        public String request(ParamPair paramPair) throws AdornerException {
            StringBuilder request = new StringBuilder();

            request.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_REQUEST_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            StringUtils.isEmpty(paramPair.table()) ? EMPTY_TABLE : paramPair.table()
                    );

            return request.toString();
        }

        @Override
        public String response(ParamPair paramPair) throws AdornerException {
            StringBuilder response = new StringBuilder();

            response.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_RESPONSE_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            StringUtils.isEmpty(paramPair.table()) ? EMPTY_TABLE : paramPair.table()
                    ).append(LINE_BREAK);

            return response.toString();
        }
    },
    /**
     * 表格为请求参数，json报文为示例
     */
    TABLE_JSON {
        @Override
        public String request(ParamPair paramPair) throws AdornerException {
            StringBuilder request = new StringBuilder();

            request.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_REQUEST_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            StringUtils.isEmpty(paramPair.table()) ? EMPTY_TABLE : paramPair.table()
                    );

            request.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_REQUEST_EXAMPLE)
                    )
                    .append(LINE_BREAK)
                    .append(
                            MarkdownUtils.jsonCode(paramPair.json())
                    ).append(NEWLINE);

            return request.toString();
        }

        @Override
        public String response(ParamPair paramPair) throws AdornerException {
            StringBuilder response = new StringBuilder();

            response.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_RESPONSE_PARAM)
                    ).append(LINE_BREAK)
                    .append(
                            StringUtils.isEmpty(paramPair.table()) ? EMPTY_TABLE : paramPair.table()
                    ).append(NEWLINE);

            response.append(MARKDOWN_ITEM)
                    .append(
                            AdornerProperty.INSTANCE.get(SPRING_ADORNER_API_RESPONSE_EXAMPLE)
                    ).append(LINE_BREAK)
                    .append(
                            MarkdownUtils.jsonCode(paramPair.json())
                    ).append(LINE_BREAK);

            return response.toString();
        }
    }

    ;


}